var moment = require('moment.js')
const formatTime = date => {
    const year = date.getFullYear()
    const month = date.getMonth() + 1
    const day = date.getDate()
    const hour = date.getHours()
    const minute = date.getMinutes()
    const second = date.getSeconds()

    return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
    n = n.toString()
    return n[1] ? n : '0' + n
}

function createDate(date) {
    var ps = date.split(" ");
    var pd = ps[0].split("-");
    var pt = ps.length > 1 ? ps[1].split(":") : [0, 0, 0];
    return new Date(pd[0], pd[1] - 1, pd[2], pt[0], pt[1], pt[2]);
}

function formatUTCDate(dateStr) {
    return moment(dateStr).format('YYYY-MM-DD HH:mm:ss')
}

function formatUTCDateByFormatStr(dateStr, formatStr) {
    return moment(dateStr).format(formatStr)
}
function generateTimeStrWithStartTimeAndGap(startTime, gapTime) {
    return moment(startTime, 'HH:mm').add(gapTime, 'minutes').format('HH:mm')
}
///根据起始时间和给定的时长，计算结束时间 startTime 起始时间字符串， 给定的时间长度单位分钟，时间格式入YYYY-MM-DD HH:mm:ss
function generateTimeStrWithStartTimeAndGapWithFormat(startTime, gapTime, timeFormate) {
    return moment(startTime, timeFormate).add(gapTime, 'minutes').format(timeFormate)
}

function combineArray() {
    var array = []
    for (var i = 0; i < arguments.length - 1; i++) {
        for (var j = 0; j < arguments.length - i - 1; j++) {
            var tempArr
            if (arguments[j + 1].length > arguments[j].length) {
                tempArr = arguments[j + 1];
                arguments[j + 1] = arguments[j];
                arguments[j] = tempArr;
            }
        }
    }
    for (var i = 0; i < arguments.length; i++) {
        array.push.apply(array, arguments[i])
    }
    return array
}
///把字典转换为字符串 dict 字典 seperator key=value间的连接符 
function generateParamsWithDict(dict, seperator) {
    var array = []
    for (var key in dict) {
        array.push(key + "=" + dict[key])
    }
    return array.join(seperator)
}
// function priceFormat(price){
//   if(price>10000){

//   }
// }
function limitStr(str, length) {
    let maxLength = length;
    let currLength = str.length;
    if (currLength >= maxLength) {
        return str.substring(0, maxLength) + '...';
    }
    else {
        return str;
    }
}

module.exports = {
    formatTime: formatTime,
    formatUTCDate: formatUTCDate,
    combineArray: combineArray,
    formatUTCDateByFormatStr: formatUTCDateByFormatStr,
    limitStr: limitStr,
    // 根据起始时间和给定的时长，计算结束时间
    generateTimeStrWithStartTimeAndGap: generateTimeStrWithStartTimeAndGap,
    // 根据起始时间和给定的时长，计算结束时间 需指定时间格式
    generateTimeStrWithStartTimeAndGapWithFormat: generateTimeStrWithStartTimeAndGapWithFormat,
    ///把字典转换为字符串
    generateParamsWithDict: generateParamsWithDict,
    //根据字符串生成日期对象
    createDate: createDate
}
